package com.siloam.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.siloam.module.Navigation;

public class NavDao {
	private SqlSession sqlSession;

	public SqlSession getSqlSession() {
		return sqlSession;
	}

	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}

	private String getMapper(String method) {
		return "com.siloam.sqlmap.NavMapper." + method;
	}
	
	public void add(Navigation nav){
		sqlSession.insert(getMapper("add"), nav);
	}
	
	public void update(Navigation nav){
		sqlSession.update(getMapper("update"), nav);
	}
	
	public void delete(String ids){
		sqlSession.delete(getMapper("delete"), ids);
	}
	
	public void delete(int id){
		sqlSession.delete(getMapper("deleteOne"), id);
	}
	
	public Navigation load(int id){
		return sqlSession.selectOne(getMapper("load"), id);
	}
	
	/**
	 * 按类型(前台/后台)查询一级导航
	 * @param type
	 * @return
	 */
	public List<Navigation> findFirstLevel(int type){
		return sqlSession.selectList(getMapper("findFirstLevel"), type);
	}

	/**
	 * 查询所有一级导航
	 * @return
	 */
	public List<Navigation> findFirstLevel(){
		return sqlSession.selectList(getMapper("findFirstLevel"));
	}
	
	/**
	 * 按类型查询二级导航
	 * @param parentId 一级导航的id
	 * @param type 
	 * @return
	 */
	public List<Navigation> findSecondLevel(int parentId, int type){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentId", parentId);
		params.put("type", type);
		return sqlSession.selectList(getMapper("findSecondLevel"), params);
	}

	/**
	 * 查询二级导航
	 * @param parentId 一级导航的id
	 * @return
	 */
	public List<Navigation> findSecondLevel(int parentId){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("parentId", parentId);
		return sqlSession.selectList(getMapper("findSecondLevel"), params);
	}
	
}
